package com.bird.ecmainmenu3.model;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.bird.ecmainmenu3.R;
import com.bird.ecmainmenu3.base.OnLoadAppsListener;
import com.bird.ecmainmenu3.util.Utils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class LoadAppsTask extends AsyncTask<Void, Void, List<AppInfo>> {
    private static final String TAG = Utils.TAG + LoadAppsTask.class.getSimpleName();

    private PackageManager mPackageManager;
    private final OnLoadAppsListener listener;
    private WeakReference<Context> mContext;
    private List<AppInfo> mAppInfoList = new ArrayList<>();

    public LoadAppsTask(@NonNull Context context, OnLoadAppsListener listener) {
        this.listener = listener;
        mContext = new WeakReference<>(context);
        mPackageManager = context.getPackageManager();
    }

    public Context getContext() {
        return mContext.get();
    }

    public String getLauncherActivity(Context context, String packageName) {
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageName);
        List<ResolveInfo> apps = mPackageManager.queryIntentActivities(resolveIntent, 0);
        if (apps.isEmpty()) {
            return "";
        }
        ResolveInfo resolveInfo = apps.iterator().next();
        if (resolveInfo != null) {
            String className = resolveInfo.activityInfo.name;
            Log.d(TAG, "getLauncherActivity: " + className);
            return className;
        }

        return "";
    }

    @Override
    protected List<AppInfo> doInBackground(Void... voids) {
        List<ApplicationInfo> installedApps = mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA);
        for (ApplicationInfo appInfo : installedApps) {
            String packageName = appInfo.packageName;
            String pkg = Utils.getAppData(getContext(), "pkg_" + packageName);
            if (!"".equals(pkg)) {
                String cls = Utils.getAppData(getContext(),"cls_" + packageName);
                createAppInfo(appInfo, packageName,cls);
            } else if (Utils.APP_LIST.contains(packageName)) {
                String cls = Utils.APP_MAP.get(packageName);
                if (cls != null && !cls.isEmpty()) {
                    createAppInfo(appInfo, packageName,cls);
                    Utils.saveAppData(getContext(), "pkg_" + packageName, packageName);
                    Utils.saveAppData(getContext(), "cls_" + packageName, cls);
                }
            }
        }

        return mAppInfoList;
    }

    @Override
    protected void onPostExecute(List<AppInfo> apps) {
        super.onPostExecute(apps);
        listener.onLoadFinished(apps);
    }

    private void createAppInfo(@NonNull ApplicationInfo appInfo, String packageName,String cls) {
        Drawable drawable = appInfo.loadIcon(mPackageManager);
        String label = appInfo.loadLabel(mPackageManager).toString();
        if (packageName.equals(Utils.SETTINGS_PKG)) {
            label = getContext().getResources().getString(R.string.car_setting);
            drawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_apps_setting_system);
        } else if (packageName.equals(Utils.MEDIA_PKG)) {
            label = getContext().getResources().getString(R.string.usb_music);
            drawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_apps_music);
        } else if (packageName.equals(Utils.AIR_PKG)) {
            drawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_apps_air_conditioner);
        }

        if (cls == null || cls.isEmpty()) {
            cls = Utils.APP_MAP.get(packageName);
        }

        mAppInfoList.add(new AppInfo(drawable, label, packageName, cls));
    }
}
